/*******************************************************************************
 * Copyright 2011 Mauro Luigi Drago (drago.luigi@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.bytenose.extrautils.patterns.dispatcher;

import java.lang.reflect.InvocationTargetException;

import com.bytenose.extrautils.annotations.NonNullable;

/**
 * A polymorphic dispatcher to invoke single methods by using reflection.
 * 
 * @author Mauro Luigi Drago
 * 
 * @param <RT>
 *            the type of the return parameter.
 */
public interface IDispatcher<RT> {
	/**
	 * Invokes the method pointed by this dispatcher on the specified target
	 * object. The method may or may not have a return value. If the invoked
	 * method has not a return value or the method returns null, this method
	 * returns null.
	 * 
	 * @param target
	 *            the target object, cannot be null
	 * @param params
	 *            the parameters, may be empty.
	 * @return the return value, may be null.
	 * @throws IllegalArgumentException
	 *             if some of the {@link NonNullable} arguments are null.
	 * @throws InvocationTargetException
	 *             if the invoked method threw an exception.
	 * @throws DispatchingException
	 *             if an error occurred invoking the method pointed by this
	 *             dispatcher.
	 * @throws NoSuchMethodException
	 *             if no method has been found for invocation.
	 */
	RT invoke(@NonNullable Object target, Object... params)
			throws IllegalArgumentException, InvocationTargetException,
			DispatchingException, NoSuchMethodException;

	/**
	 * Invokes the static method pointed by this dispatcher on the specified
	 * target class. The method may or may not have a return value. If the
	 * invoked method has not a return value or the method returns null, this
	 * method returns null. If the method is not static, a
	 * {@link DispatchingException} is raised.
	 * 
	 * @param target
	 *            the target class, cannot be null.
	 * @param params
	 *            the parameters, may be empty.
	 * @return the return value, may be null.
	 * @throws IllegalArgumentException
	 *             if some of the {@link NonNullable} arguments are null.
	 * @throws InvocationTargetException
	 *             if the invoked method threw an exception.
	 * @throws DispatchingException
	 *             if an error occurred invoking the method pointed by this
	 *             dispatcher.
	 * @throws NoSuchMethodException
	 *             if no method has been found for invocation.
	 */
	RT invokeStatic(@NonNullable Class<?> target, Object... params) 
			throws IllegalArgumentException, InvocationTargetException, 
			DispatchingException, NoSuchMethodException;

	/**
	 * Invokes the method pointed by this dispatcher on the specified target
	 * object. The method may or may not have a return value. If the invoked
	 * method has not a return value or the method returns null, this method
	 * returns null.
	 * 
	 * @param target
	 *            the target object, cannot be null.
	 * @param params
	 *            the parameters, may be empty.
	 * @return the return value, may be null.
	 * @throws IllegalArgumentException
	 *             if some of the {@link NonNullable} arguments are null.
	 * @throws InvocationTargetException
	 *             if the invoked method threw an exception.
	 */
	RT invokeSwallow(@NonNullable Object target, Object... params)
			throws IllegalArgumentException, InvocationTargetException;

	/**
	 * Invokes the method pointed by this dispatcher on the specified target
	 * object. The method may or may not have a return value. If the invoked
	 * method has not a return value or the method returns null, this method
	 * returns null. If the method is not static, a {@link DispatchingException}
	 * is raised.
	 * 
	 * @param target
	 *            the target class, cannot be null.
	 * @param params
	 *            the parameters, may be empty.
	 * @return the return value, may be null.
	 * @throws IllegalArgumentException
	 *             if some of the {@link NonNullable} arguments are null.
	 * @throws InvocationTargetException
	 *             if the invoked method threw an exception.
	 */
	RT invokeStaticSwallow(@NonNullable Class<?> target, Object... params)
			throws IllegalArgumentException, InvocationTargetException;
}
